package com.foxzzz.link;


import com.foxzzz.base.ListNode;

public class Solution21 {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        //前置节点 除去恶心的判空处理
        ListNode head = new ListNode(-1);
        //游标
        ListNode prev = head;
        while (true) {
            if (l1 == null || l2 == null) {
                break;
            }
            if (l1.val > l2.val) {
                prev.next = l2;
                l2 = l2.next;
            } else {
                prev.next = l1;
                l1 = l1.next;
            }
            prev = prev.next;
        }
        //续尾
        prev.next = l1 == null ? l2 : l1;

        return head.next;
    }

    public static void main(String[] args) {
        Solution21 solution21 = new Solution21();
        ListNode listNode = solution21.mergeTwoLists(ListNode.getListNode("[1,2,4]"), ListNode.getListNode("[1,3,4]"));
        ListNode.visit(listNode);
    }
}
